# Maintainer: Rémi BERTHO <remi.bertho@dalan.fr>

_realname=podofo
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-tools"))
pkgver=0.10.4
pkgrel=2
pkgdesc="A C++ library to work with the PDF file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://podofo.sourceforge.io/"
msys2_repository_url="https://github.com/podofo/podofo"
msys2_references=(
  "cpe: cpe:/a:podofo_project:podofo"
)
license=('spdx:LGPL-2.0-or-later AND GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libunistring"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/podofo/podofo/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-mingw-build-tools.patch::https://github.com/podofo/podofo/pull/155.patch"
        "002-fix-redefinitions.patch"
        "003-aarch64.patch"
        "004-std-from-chars.patch")
sha256sums=('6b1b13cdfb2ba5e8bbc549df507023dd4873bc946211bc6942183b8496986904'
            'dd85ee9e8b89fabfdad745d67e97bab4bed4734e3535ad1753361a35c32681ab'
            '98a936f042c1a5ba9a3c8c89c4384444fc6bf47a7c5ce59de551cce225eac719'
            'aa1d6d3084eaf3a6496762708523edd775a92b2ac413f16626d882b1d20ec04b'
            'd7f72157000be6c122d29803778aba65eb783e1fd562b04d5a5e0f7bcd50ed0f')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  # https://github.com/podofo/podofo/issues/152
  apply_patch_with_msg \
    001-mingw-build-tools.patch

  # https://github.com/podofo/podofo/pull/155
  apply_patch_with_msg \
    002-fix-redefinitions.patch

  apply_patch_with_msg \
    003-aarch64.patch \
    004-std-from-chars.patch
}

build() {
  declare -a _cmake_opts=(
    -GNinja
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}"
    -DPODOFO_HAVE_JPEG_LIB=ON
    -DPODOFO_HAVE_PNG_LIB=ON
    -DPODOFO_HAVE_TIFF_LIB=ON
    -DPODOFO_HAVE_FONTCONFIG=ON
    -DPODOFO_BUILD_EXAMPLES=OFF
    -DPODOFO_BUILD_TEST=OFF
  )
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    _cmake_opts+=("-DPODOFO_BUILD_TOOLS=ON")
  fi

  if check_option "debug" "n"; then
    _cmake_opts+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _cmake_opts+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export CFLAGS+=" -Wno-deprecated-declarations -Wno-deprecated-literal-operator -Wno-switch"
  export CXXFLAGS+=" -Wno-deprecated-declarations -Wno-deprecated-literal-operator -Wno-switch"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    "${_cmake_opts[@]}" \
    -DPODOFO_BUILD_STATIC=ON \
    -S ${_realname}-${pkgver} \
    -B "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    "${_cmake_opts[@]}" \
    -DPODOFO_BUILD_STATIC=OFF \
    -S ${_realname}-${pkgver} \
    -B "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-shared"
}

package_podofo() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-shared"

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    mkdir -p "dest${MINGW_PREFIX}/bin"
    mv "${pkgdir}${MINGW_PREFIX}"/bin/*.exe "dest${MINGW_PREFIX}/bin"
  fi
}

package_podofo-tools() {
  pkgdesc+=" (Tools)"
  depends=("${MINGW_PACKAGE_PREFIX}-podofo=${pkgver}-${pkgrel}")

  mv dest/* "${pkgdir}"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1/" "${_realname}-${pkgver}"/man/*.1
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
